package com.jeek.jdbc.myCode.homeWork2;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


public class BaseDao {
    /**
     * 查询常量
     * @param conn  连接对象
     * @param sql   SQL语句
     * @param args  查询的条件
     * @return
     * @throws SQLException
     */
    public Object selectConst(Connection conn, String sql,Object... args) throws SQLException {
        PreparedStatement ppst = conn.prepareStatement(sql);
        //给？占位符传值
        for (int i = 0; i < args.length; i++) {
            ppst.setObject(i + 1, args[i]);
        }
        //执行查询操作
        ResultSet rs = ppst.executeQuery();
        while (rs.next()) {
            Object object = rs.getObject(1);
            return object;
        }
        return null;
    }

    /**
     * 查询单个信息
     * @param conn  连接对象
     * @param sql   SQL语句
     * @param args  查询的条件
     * @return
     * @throws SQLException
     */
    public  Customer selectCustomer(Connection conn, String sql,Object... args) throws SQLException {
        Customer customer = null;
        PreparedStatement ppst = conn.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            ppst.setObject(i + 1, args[i]);
        }
        ResultSet rs = ppst.executeQuery();
        while (rs.next()) {
             customer = new Customer();
            customer.setId(rs.getInt(1));
            customer.setName(rs.getString(2));
            customer.setEmail(rs.getString(3));
            String string = rs.getString(4);
            LocalDate birthday = LocalDate.parse(string, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            customer.setBirthday(string);

        }
        return customer;
    }
    /**
     * 查询列表信息
     * @param conn  连接对象
     * @param sql   SQL语句
     * @param args  查询的条件
     * @return
     * @throws SQLException
     */
    public List<Customer> selectAll(Connection conn, String sql , Object... args) throws SQLException {
        List<Customer> list = new ArrayList<>();
        PreparedStatement ppst = conn.prepareStatement(sql);
        //给?占位符传递值
        for (int i = 0; i < args.length; i++) {
            ppst.setObject(i+1 , args[i]);
        }
        //执行查询操作
        ResultSet rs = ppst.executeQuery();
        while (rs.next()) {
                Customer customer = new Customer();
            customer = new Customer();
            customer.setId(rs.getInt(1));
            customer.setName(rs.getString(2));
            customer.setEmail(rs.getString(3));
            String string = rs.getString(4);
            LocalDate birthday = LocalDate.parse(string, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            customer.setBirthday(string);
            list.add(customer);
        }
        return list;
    }

    /**
     * 增删改
     * @param conn   连接对象
     * @param sql    执行DML的SQL语句
     * @param args   条件
     * @return       返回实际受影响行数
     * @throws SQLException
     */
    public int update(Connection conn, String sql , Object... args) throws SQLException {
        PreparedStatement ppst = conn.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            ppst.setObject(i+1 , args[i]);
        }
        int i = ppst.executeUpdate();
        return i;
    }


}